import calendar
import datetime
import json
import sys
import time
import uuid

import requests
import queue

file = open("test.txt", mode="a", encoding="utf-8")


def zhifu(weight, foodid, ji, deviceid):
    uuid_pbj = uuid.uuid4()
    uuid_str = uuid_pbj.hex

    now = time.gmtime()
    times = calendar.timegm(now)

    print("结算id")
    url1 = "http://192.168.1.30:8500/api/terminal/self/weigh/add"
    headers1 = {
        'Content-Type': "application/json;charset=UTF-8",
        "Authorization": "Bearer 4ee7e7de-cc18-4e73-b55e-a2b4f393bef6"
    }
    data1 = {
        "canteenId": "8a7f806e8bf5f951018bf5fb13fc0000",
        "companyId": "8a7f806e8bad9fff018bf5f24b280000",
        "deviceId": deviceid,
        "uniqueNo": uuid_str,
        "amountMode": 0,
        "orderPrice": "1.5",
        "payMethod": 1,
        "code": "KDBYL000000017782",
        "otherPayMethodId": "",
        "trayChipNo": "85427E64500104E0",
        "foods": [{
            "id": foodid,
            "number": 1,
            "numberGram": weight,
            "priceCompany": 1
        }],
        "weighLogVos": [{
            "createTime": times,
            "residueWeight": 0,
            "type": 1
        }, {
            "createTime": times,
            "residueWeight": -weight,
            "type": 2
        }]
    }
    data1_json = json.dumps(data1)
    res_one = requests.request("post", url=url1, headers=headers1, data=data1_json).json()
    res_one_json = json.dumps(res_one)
    now_time1 = datetime.datetime.now()
    if res_one['status'] == 200:
        file.write(str(now_time1) + "第" + ji + "笔结算成功，绑定接口返回值" + res_one_json + "\n")
        print("第" + ji + "笔结算成功")

    else:
        file.write(str(now_time1) + "第" + ji + "笔结算失败，绑定接口返回值" + res_one_json + "\n")
        print("第" + ji + "笔结算失败")


def sel():
    time.sleep(2)
    url = "http://192.168.1.30:8500/api/terminal/self/weigh/get/card/bind"
    header = {
        "Authorization": "Bearer 4ee7e7de-cc18-4e73-b55e-a2b4f393bef6",
        "Content-Type": "application/json;charset=UTF-8"
    }
    data = {

        "cardId": "KDBYL000000017782",
        "trayChipNo": "85427E64500104E0",
        "companyId": "8a7f806e8bad9fff018bf5f24b280000",
        "canteenId": "8a7f806e8bf5f951018bf5fb13fc0000"

    }
    data_json = json.dumps(data)
    res = requests.request("POST", url=url, headers=header, data=data_json).json()
    res_json = json.dumps(res)
    print(res)
    if res["message"] is None:
        now_time = datetime.datetime.now()
        file.write(str(now_time) + "查询成功，查询接口返回值" + res_json + "\n")
        print("查询成功")
    elif res["status"] == 4004:
        now_time = datetime.datetime.now()
        file.write(str(now_time) + "查询失败，餐卡未绑定餐盘，查询接口返回值" + res_json + "\n")
        print("查询失败")
        sys.exit()
    else:
        print(res_json)
        sys.exit()


def tary():
    url = "http://192.168.1.30:8500/api/terminal/self/weigh/del/tray"
    header = {
        "Authorization": "Bearer 4ee7e7de-cc18-4e73-b55e-a2b4f393bef6",
        "Content-Type": "application/json;charset=UTF-8"
    }
    data = {
        "trayChipNo": "85427E64500104E0",
        "canteenId": "8a7f806e8bf5f951018bf5fb13fc0000",
        "companyId": "8a7f806e8bad9fff018bf5f24b280000"
    }
    data_json = json.dumps(data)
    res = requests.request("POST", url=url, headers=header, data=data_json).json()
    res_json = json.dumps(res)
    if  res['status'] == 200:
        now_time = datetime.datetime.now()
        file.write(str(now_time) + "解绑成功，解绑接口返回值" + res_json + "\n")
        print("解绑成功")
    else:
        print("解绑失败")


def start():
    # 取餐卡id
    # url = "http://192.168.1.29:8500/api/web/mealcard/page"
    # methon = "POST"
    # headers = {
    #    "Authorization": "Bearer 63ac27b1-73d5-4ff0-bf20-9c0e05c53ad1",
    #    "Content-Type" : "application/json;charset=UTF-8",
    #    "Accept" : "application/json, text/plain, */*"
    # }
    # headers_json = json.dumps(headers)

    # for i in range (1,10):
    #     data = {"canteenId": "", "cardNo": "", "mealCardTypeId": "", "memberCategoryIds": [], "name": "", "account": "",
    #             "status": "", "type": "", "encrypt": "", "pageNum": i, "pageSize": 1000}
    #     data_json = json.dumps(data)
    #     res = requests.request(method=methon,url=url,headers=headers,data=data_json).json()
    #
    #     for item in res['data']:
    #         s.put(item['cardId'])

    # 绑盘验证
    while True:
        url = "http://192.168.1.30:9900/terminal/self/weigh/upd/bind/card/plate"
        methon = "POST"
        headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer 4ee7e7de-cc18-4e73-b55e-a2b4f393bef6"
        }
        data = {
            "cardId": "KDBYL000000017782",
            "trayChipNo": "85427E64500104E0",
            "companyId": "8a7f806e8bad9fff018bf5f24b280000",
            "canteenId": "8a7f806e8bf5f951018bf5fb13fc0000"
        }
        data_json = json.dumps(data)
        res = requests.request(method=methon, url=url, headers=headers, data=data_json).json()
        res_json = json.dumps(res)
        if res['status'] == 200:
            now_time = datetime.datetime.now()
            file.write(str(now_time) + "绑盘成功，绑定接口返回值" + res_json + "\n")

            print("绑盘成功")
        else:
            print("绑盘失败")
            now_time = datetime.datetime.now()
            file.write(str(now_time) + "绑盘失败，绑定接口返回值" + res_json + "\n")

        sel()
        time.sleep(5)
        zhifu(weight=30, foodid="8a7f806e8c77b65d018c800395ac0047", ji="1", deviceid="YQ2022070205")
        sel()
        time.sleep(5)
        zhifu(weight=40, foodid="8a7f806e8fa373b1018fb9401c592440", ji="2", deviceid="123456")
        sel()
        time.sleep(5)
        zhifu(weight=50, foodid="8a7f806e8fa373b1018fb7d91c662814", ji="3", deviceid="1234567")
        time.sleep(3)

        tary()



if __name__ == '__main__':
    start()
